package com.study.Controller;

import com.common.Entity.Result;
import com.github.pagehelper.PageInfo;
import com.study.Entity.Category;
import com.study.Service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/getCategoryList")
    public Result getCategoryList(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(required = false) String categoryName) {
        PageInfo<Category> pageInfo = categoryService.getCategoryList(pageNum, pageSize, categoryName);
        return Result.success(pageInfo);
    }

    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }

    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    @DeleteMapping("/deleteCategory/{categoryId}")
    public Result deleteCategory(@PathVariable("categoryId") String categoryId) {
        categoryService.deleteCategory(categoryId);
        return Result.success();
    }

    @GetMapping("/getAllCategory")
    public Result getAllCategory() {
        return Result.success(categoryService.getAllCategory());
    }
}
